import datetime
import geo.geomodel
import logging as log
import cgi
import os
import datetime
import random
import geo.geomath as geomath
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from django.utils import simplejson as json

class Business (db.Model):
    
	name = db.StringProperty(required=True)
	location = db.GeoPtProperty(required=True)
	category = db.StringProperty(required=True)
	subcategory = db.StringProperty(required=True)
	picture = db.StringProperty()
	area_key = db.StringProperty(required=True)
	
	@staticmethod
	def Create(geoPt, name1, cat, sub, pic,akey,self):
		business_report = Business(name = name1, location=geoPt, category=cat, subcategory=sub, picture=pic , area_key=akey)
		business_report.put()
		self.response.out.write(str(business_report.key()))

class AddBusinessHandler(webapp.RequestHandler):
    """
	/add_business?name=Taj Mahal&pic=http://www.isc.tamu.edu/~lewing/linux/sit3-shine.7.gif&loc=0.18352,2.845912&cat=Places&sub=Tour
	"""
	
    def get(self):
        
		name1 = self.request.get("name")
		location1 = self.request.get("loc")
		picture1 = self.request.get("pic")
		category1 = self.request.get("cat")
		subcategory1 = self.request.get("sub")
		akey = self.request.get("akey")
       
		if (name1 and location1 and picture1 and category1 and subcategory1 and akey):
			Business.Create(location1, name1,category1,subcategory1,picture1,akey,self)
		else:
			self.response.out.write("not all params were given")
			return 
		
class GetBusinessHandler(webapp.RequestHandler):
    """
	/get_business?bkey=ag9kZXZ-YW5nZWxzLWRlbW9yCgsSBEFyZWEYAQw
	"""
    def get(self):
        
		bkey = self.request.get("bkey")
		business = Business.get(bkey)
		if (business):
			json_user = {
			'name': business.name,
			'loc' : str(business.location.lat)+","+str(business.location.lon),
			'pic' : business.picture,
			'cat' : business.category,
			'sub' : business.subcategory,
			'akey' : business.area_key
			}
			self.response.out.write(json.dumps(json_user))			